package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.context.BaseContext;
import com.example.mapper.StageMapper;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.Project;
import com.example.pojo.entity.Stage;
import com.example.pojo.vo.StageVO;
import com.example.service.AdminService;
import com.example.service.StageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author ding
 * @since 2024/6/11
 */
@Service
@DS("master")
public class StageServiceImpl extends ServiceImpl<StageMapper, Stage> implements StageService {
    @Autowired
    StageMapper stageMapper;

    @Autowired
    ProjectServiceImpl projectServicel;

    @Autowired
    private AdminService adminService;

    public Page<Stage> selectPage(String projectName, String order, Integer projectId, Integer pageNum, Integer pageSize) {
        Admin admin = adminService.selectById(Math.toIntExact(BaseContext.getCurrentId()));
        admin = adminService.selectById(admin.getId());
        QueryWrapper<Stage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0)
                .eq(ObjectUtil.isNotEmpty(projectId), "project_id", projectId);

        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.eq("team_id",admin.getTeamId());
        List<Project> list = projectServicel.list(projectQueryWrapper);
        List<Integer> IdList = list.stream().map(p -> p.getId()).collect(Collectors.toList());
        queryWrapper.in("project_id",IdList);

        if (projectName != null){
            Project project = projectServicel.selectByName(projectName);
            queryWrapper.eq("project_id",project.getId());
        }

        if (order != null){
            if (order.equals("ascend")){
                queryWrapper.orderByAsc("sort");
            }
            if (order.equals("descend")){
                queryWrapper.orderByDesc("sort");
            }
        }

        Page<Stage> stagePage = stageMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        List<Stage> records = stagePage.getRecords();
        for (Stage record : records) {
            record.setProjectName(projectServicel.getById(record.getProjectId()).getName());
        }
        stagePage.setRecords(records);
        return stagePage;
    }

    public void updateStage(Stage stage) {
        QueryWrapper<Stage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",stage.getProjectId());
        List<Stage> stageList = list(queryWrapper);

        if (stage.getProgress() == 100){
            stage.setComplete(LocalDate.now());
            stage.setIsDeleted(0);
            stage.setNumber(stageList.size() + 1);
        }

        stageMapper.updateById(stage);
    }

    public void deleteById(Integer id) {
        Stage stage = getById(id);
        stage.setIsDeleted(1);
        updateStage(stage);
    }

    public void delBatch(List<Integer> ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }

    /**
     * 为项目增加新阶段
     */
    public void addStage(Stage stage) {
        QueryWrapper<Stage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",stage.getProjectId());
        List<Stage> stageList = list(queryWrapper);

        stage.setIsDeleted(0);
        stage.setSort(stageList.size() + 1);
        stage.setIsDeleted(0);
        stage.setPrice(BigDecimal.valueOf(0));
        stage.setProgress(0);
        stage.setVersion(0);

        stageMapper.insert(stage);
    }

    /**
     * 根据项目id获取相关阶段信息
     */
    public StageVO selectByProjectId(Integer projectId) {
        List<Stage> stages = stageMapper.selectList(new LambdaQueryWrapper<Stage>()
                .eq(Stage::getProjectId, projectId)
                .orderByAsc(Stage::getSort));

        StageVO stageVO = new StageVO();
        stageVO.setProjectName(projectServicel.selectById(projectId).getName());
        if (stages.size() > 0) {
            stageVO.setStage1(stages.get(0));
        }
        if (stages.size() > 1) {
            stageVO.setStage2(stages.get(1));
        }
        if (stages.size() > 2) {
            stageVO.setStage3(stages.get(2));
        }
        if (stages.size() > 3) {
            stageVO.setStage4(stages.get(4));
        }
        return stageVO;
    }

}
